import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from './router.config.js'
import { getToken } from '@/utils/auth'
import { toast } from '@/utils/index'

// hack router replace callback
const originalReplace = Router.prototype.replace
Router.prototype.replace = function (location) {
  return originalReplace.call(this, location).catch(err => err)
}

// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const createRouter = () =>
  new Router({
    // mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath
    // base: process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// router.beforeEach((to, from, next) => {
//   // 定义无需登录的页面
//   let _pagenologin = {"index":true,"Company":true,"Login":true,'FPIntroduction':true,'FPBtc':true,'Manage':true,'Asset':true,'News':true,'NewsDetail':true,'Global':true}
//
//   //移动设备判断
//   const path = to.path; // 获取目标路径
//   const pathName = path.split('/',2)[1]; // 提取路径的主要部分
//   const isPhonePath = pathName.startsWith('p'); // 判断路径是否为移动端路径
//   const isPhone = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i) // 检测是否为移动设备
//
//   // 根据设备类型跳转至对应版本
//   if (isPhone && !isPhonePath) { // 如果是移动设备且路径不是移动端路径
//     const name = pathName.charAt(0).toUpperCase() + pathName.substring(1)
//     next('/'+'p'+ name) // 跳转到移动端路径
//   }
//   if(!isPhone && isPhonePath){ // 如果是PC设备且路径是移动端路径
//     const name = pathName.charAt(1).toLowerCase() + pathName.substring(2)
//     next('/' + name) // 跳转到PC端路径
//   }
//   next()
// })

router.beforeEach((to, from, next) => {
  // 定义无需登录的页面
  let _pagenologin = {"index": true, "Company": true, "Login": true, 'FPIntroduction': true, 'FPBtc': true, 'Manage': true, 'Asset': true, 'News': true, 'NewsDetail': true, 'Global': true};
  next();
});


export default router
